/*
 */
package beans;

import db_connect.DB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Milan Boricic <boricic@gmail.com>
 */
public class ZahtevRegistracije {

    private Integer zid;
    private String username;
    private String lozinka;
    private String lozinka2;
    private String ime;
    private String prezime;
    private String email;
    private String funkcija;
    private Timestamp vremeZahteva;
    private Timestamp vremeOdluke;
    private Integer status; // 0 - neobradjen, 1 - prihvacen, 2 - odbijen

    private boolean admin;

    /** Creates a new instance of ZahtevRegistracije */
    public ZahtevRegistracije() {
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFunkcija() {
        return funkcija;
    }

    public void setFunkcija(String funkcija) {
        this.funkcija = funkcija;
    }

    public String getIme() {
        return ime;
    }

    public void setIme(String ime) {
        this.ime = ime;
    }

    public String getLozinka() {
        return lozinka;
    }

    public void setLozinka(String lozinka) {
        this.lozinka = lozinka;
    }

    public String getPrezime() {
        return prezime;
    }

    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Timestamp getVremeOdluke() {
        return vremeOdluke;
    }

    public void setVremeOdluke(Timestamp vremeOdluke) {
        this.vremeOdluke = vremeOdluke;
    }

    public Timestamp getVremeZahteva() {
        return vremeZahteva;
    }

    public void setVremeZahteva(Timestamp vremeZahteva) {
        this.vremeZahteva = vremeZahteva;
    }

    public Integer getZid() {
        return zid;
    }

    public void setZid(Integer zid) {
        this.zid = zid;
    }

    public String getLozinka2() {
        return lozinka2;
    }

    public void setLozinka2(String lozinka2) {
        this.lozinka2 = lozinka2;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public String register() {
        String result = null;

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        FacesContext context = FacesContext.getCurrentInstance();

        if (lozinka.compareTo(lozinka2) != 0) {
            context.addMessage(null, new FacesMessage("Lozinka i ponovljena lozinka nisu iste!"));
        } else {
            try {
                con = DB.getConnection();
                stmt = con.createStatement();

                String query = "select * from zahtevi_registracije where email = '" + email + "';";
                rs = stmt.executeQuery(query);

                if(rs.next()) {
                    Short st = rs.getShort("status");
                    if(st == 0) {
                        context.addMessage(null, new FacesMessage("Zahtev korisnika sa navedeim e-mail-om je već poslat, ali još nije obrađen od strane administratora."));
                    } else if(st == 1) {
                        context.addMessage(null, new FacesMessage("Korisnik sa navedenim e-mail-om već postoji!"));
                    } else if(st == 2) {
                        context.addMessage(null, new FacesMessage("Vaš zahtev je odbijen od strane administratora. Nemate više pravo da se registrujete."));
                    }
                } else {
                    query = "select * from zahtevi_registracije where username = '" + username + "';";
                    ResultSet rs2 = stmt.executeQuery(query);
                    if(rs2.next()) {
                        String tmpUsername = rs2.getString("username");
                        context.addMessage(null, new FacesMessage("Korisničko ime '" + tmpUsername + "' već postoji!"));
                    } else {
                        Timestamp sada = new Timestamp(System.currentTimeMillis());
                        query = "insert into zahtevi_registracije(username, lozinka, ime, prezime, email, funkcija, vreme_zahteva, status)" +
                                "values ('" + username + "','" + lozinka + "','" + ime + "','" + prezime + "','" + email + "','" + funkcija + "','" + sada + "', 0);";

                        stmt.executeUpdate(query);

                        result = "register_ok";
                    }
                }

                stmt.close();
                con.close();

            } catch (SQLException ex) {
                result = "error";
                ex.printStackTrace();
                if (con != null) {
                    try {
                        con.close();
                    } catch (Exception exc) {
                    }
                }
            }
        }

        return result;
    }

    public String kreirajNovogKorisnika() {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        FacesContext context = FacesContext.getCurrentInstance();

        if (lozinka.compareTo(lozinka2) != 0) {
            context.addMessage(null, new FacesMessage("Lozinka i ponovljena lozinka nisu iste!"));
        } else {
            try {
                con = DB.getConnection();
                stmt = con.createStatement();

                String query = "select * from korisnik where username = '" + username + "';";
                rs = stmt.executeQuery(query);


                ResultSet rs2 = stmt.executeQuery(query);
                if (rs2.next()) {
                    String tmpUsername = rs2.getString("username");
                    context.addMessage(null, new FacesMessage("Korisničko ime '" + tmpUsername + "' već postoji!"));
                } else {
                    query = "insert into korisnik(username, lozinka, ime, prezime, email, funkcija, isAdmin)" +
                            "values ('" + username + "','" +
                            lozinka + "','" +
                            ime + "','" +
                            prezime + "','" +
                            email + "','" +
                            funkcija + "'," +
                            admin + ");";

                    stmt.executeUpdate(query);
                    context.addMessage(null, new FacesMessage("Korisnik uspešno kreiran!"));

                }

                stmt.close();
                con.close();

            } catch (SQLException ex) {
                ex.printStackTrace();
                if (con != null) {
                    try {
                        con.close();
                    } catch (Exception exc) {
                    }
                }
            }
        }

        return "admin";
    }

    public List<ZahtevRegistracije> getZahtevRegistracijeByUpit(String query) {
        ArrayList<ZahtevRegistracije> zahtevi = null;

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            zahtevi = new ArrayList<ZahtevRegistracije>();
            while (rs.next()) {
                ZahtevRegistracije zahtev = new ZahtevRegistracije();
                zahtev.setZid(rs.getInt("zid"));
                zahtev.setUsername(rs.getString("username"));
                zahtev.setLozinka(rs.getString("lozinka"));
                zahtev.setIme(rs.getString("ime"));
                zahtev.setPrezime(rs.getString("prezime"));
                zahtev.setEmail(rs.getString("email"));
                zahtev.setFunkcija(rs.getString("funkcija"));
                zahtev.setVremeZahteva(rs.getTimestamp("vreme_zahteva"));
                zahtev.setVremeOdluke(rs.getTimestamp("vreme_odluke"));
                zahtev.setStatus(rs.getInt("status"));

                zahtevi.add(zahtev);
            }

            stmt.close();
            con.close();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }

        return zahtevi;
    }

    public List getZahteviRegistracijeNeobradjeni() {
        String query = "select * from zahtevi_registracije where status = 0";
        List zahtevi = getZahtevRegistracijeByUpit(query);

        return zahtevi;
    }

    public void updateStatus(Integer status) {
        Connection con = null;
        Statement stmt = null;
        FacesContext context = FacesContext.getCurrentInstance();

        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            Timestamp sada = new Timestamp(System.currentTimeMillis());
            String query = "update zahtevi_registracije " +
                    "set status = " + status + ", " +
                    "vreme_odluke = '" + sada + "' " +
                    "where zid = " + this.zid + ";";
            int res = stmt.executeUpdate(query);

            if(res == 1) {
                context.addMessage(null, new FacesMessage("Prijava uspešno obrađena!"));
            }

            stmt.close();
            con.close();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }
    }

    public String prihvati() {
        updateStatus(1);

        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik kor = (Korisnik) session.getAttribute("korisnik");
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            String query = "insert into korisnik(username, lozinka, ime, prezime, email, funkcija, isAdmin)" +
                                "values ('" + username + "','" +
                                lozinka + "','" +
                                ime + "','" +
                                prezime + "','" +
                                email + "','" +
                                funkcija + "'," +
                                admin + ");";

            stmt.executeUpdate(query);
            stmt.close();
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }
        
        return kor.isIsAdmin() ? Korisnik.ACTION_ADMIN : Korisnik.ACTION_KORISNIK;
    }

    public String odbaci() {
        updateStatus(2);

        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik kor = (Korisnik) session.getAttribute("korisnik");

        return kor.isIsAdmin() ? Korisnik.ACTION_ADMIN : Korisnik.ACTION_KORISNIK;
    }
}
